package sizeyunsuan;

import java.io.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;


public class Calculater {


    public ArrayList<String> getStringList(String str) {
        ArrayList<String> result = new ArrayList<String>();
        StringTokenizer tokenizer = new StringTokenizer(str," ");
        while(tokenizer.hasMoreTokens()) {
            result.add(tokenizer.nextToken());

            }
            return result;
    }
    public ArrayList<String> getPostOrder(ArrayList<String> inOrderList) {

        ArrayList<String> result = new ArrayList<String>();
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < inOrderList.size(); i++) {
            if (Character.isDigit(inOrderList.get(i).charAt(0))) {
                result.add(inOrderList.get(i));
            } else {
                switch (inOrderList.get(i).charAt(0)) {
                    default:
                        while (!stack.isEmpty() && compare(stack.peek(), inOrderList.get(i))) {
                            result.add(stack.pop());
                        }
                        stack.push(inOrderList.get(i));
                        break;
                }
            }
        }
        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }
        return result;
    }


    public String calculate(ArrayList<String> postOrder)throws IOException {
        Stack stack = new Stack();
        //ArrayList<String> stack = postOrder;
        String res = "";
        FileWriter fm = new FileWriter("answer.txt");
        BufferedWriter bufm = new BufferedWriter(fm);
        for (int i = 0; i < postOrder.size(); i++) {
            if (res.equals("")) {
                if (Character.isDigit(postOrder.get(i).charAt(0))) {
                    stack.push(Integer.parseInt(postOrder.get(i)));
                } else {

                    Integer back = (Integer) stack.pop();

                    Integer front = (Integer) stack.pop();
                    stack.push(back);

                    Calculator calculator2 = new Calculator();
                    switch (postOrder.get(i).charAt(0)) {
                        case '+':

                            res = calculator2.compute(front + "/1", "+", back + "/1");
                            //res = (front + back);
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();

                            break;
                        case '-':
                            res = calculator2.compute(front + "/1", "-", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                        case '*':
                            res = calculator2.compute(front + "/1", "*", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                        case '/':
                            res = calculator2.compute(front + "/1", "/", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                    }
                }
            }
            else {
                if (Character.isDigit(postOrder.get(i).charAt(0))) {
                    stack.push(Integer.parseInt(postOrder.get(i)));
                } else {

                    Integer back = (Integer) stack.pop();
                    Calculator calculator2 = new Calculator();
                    switch (postOrder.get(i).charAt(0)) {
                        case '+':

                            res = calculator2.compute(res, "+", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                        case '-':
                            res = calculator2.compute(res, "-", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                        case '*':
                            res = calculator2.compute(res, "*", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                        case '/':
                            res = calculator2.compute(res, "/", back + "/1");
                            bufm.write(res);
                            bufm.flush();
                            bufm.newLine();
                            break;
                    }
                }

            }

            }
            return res;
        }




    public static boolean compare(String peek, String cur) {
        if ("*".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("/".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("+".equals(peek) && ("+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("-".equals(peek) && ("+".equals(cur) || "-".equals(cur))) {
            return true;
        }
        return false;
    }

   public void calc(String a) throws IOException{
        Calculater calculater = new Calculater();
        String s = a;
        ArrayList<String> result = calculater.getStringList(s);  //String转换为List
        result = calculater.getPostOrder(result);   //中缀变后缀
       calculate(result);//计算
    }
}